let QQMapWX = require('../lib/qqmap-wx-jssdk.min.js');
let config = require("../../script/data/config.js");
let auth = require("auth");
let qqmapsdk = new QQMapWX({key: config.map.appid});
let needTrans = true;
const transLocationModel = {
  latitude : "",      // 维度
  longitude : "",     // 经度
  nation: "",         // 国家
  province: "",       // 省份
  city: "",           // 城市
  district: "",       // [ 区   ]
  street: "",         // [ 街道 ]
  street_number: "",  // [ 门牌 ]
  transRaw: "",       // 源信息
}
/**
 * buildTransModel 构建转化地理位置模型
 */
function buildTransModel(res){
  return {
    latitude: res.result.location.lat,
    longitude: res.result.location.lng,
    nation: res.result.address_component.nation,
    province: res.result.address_component.province,
    city: res.result.address_component.city,
    district: res.result.address_component.district,
    street: res.result.address_component.street,
    street_number: res.result.address_component.street_number,
    transRaw: JSON.stringify(res)
  }
}

/**
 * getCity 获取城市信息
 */
function getCity(latAndLng,call){
  qqmapsdk.reverseGeocoder({
    location: latAndLng,
    coord_type:1,
    get_poi:0,
    success: function(r){
      call(r.result.address_component.city);
    }, fail: function(){
      call(config.autoCity);
    }
  })
}
module.exports.getCity = getCity;

/**
 * getLocation 获取地理位置信息
 */
function getLocation(isLocal,opition){
  if (isLocal) {
    wx.getLocation({ success: transLocation, fail: failHandler })
  } else {
    wx.chooseLocation({ success: transLocation, fail: failHandler})
  }
  function transLocation(res){
    if (needTrans){
      qqmapsdk.reverseGeocoder({
        location: { latitude: res.latitude, longitude:res.longitude},
        coord_type: 1,
        get_poi: 0,
        success: function (r) {
          opition.success(buildTransModel(r));
        }, fail: function () {
          opition.success(res);
        }
      })
    } else {
      opition.success(res);
    }
   
  }
  function failHandler(res){
    res = res || this.res;
    if (res.errMsg == "getLocation:fail auth deny" || res.errMsg == "chooseLocation:fail auth deny"){
      /** 如果失败原因未 未授权 - 唤起授权 */
      auth.getAuth("scope.userLocation", {
        success: () => {
          getLocation(isLocal, opition);
        },
        fail: failHandler.bind({ errMsg: "user cancel" }) 
      })
    } else if(res.errMsg == "user cancel"){
      /** 用户取消授权 */
      wx.showToast({ title: '用户未授权', icon: "none" })
      opition.fail();
    } else {
      /** 未知错误 */
      wx.showToast({ title: '定位失败', icon: "none" })
      console.log(res);
      opition.fail();
    }
  }
}
module.exports.getLocation = getLocation;

/**
 * calcDistance 计算经纬度距离
 */
let calcDistance = function (lat1AndLng1, lat1AndLng2, toFixed) {

  let PI = Math.PI;
  let rad1 = lat1AndLng1[0] * PI / 180.0;
  let rad2 = lat1AndLng2[0] * PI / 180.0;
  let a = rad1 - rad2;
  let b = lat1AndLng1[1] * PI / 180.0 - lat1AndLng2[1] * PI / 180.0;
  let r = 6378137;//地球半径

  let s = (
    (r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) 
      + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))).toFixed(0)
  ) / 1000;
  

  return s.toFixed(toFixed || 3);
};
module.exports.calcDistance = calcDistance;